// pages/cmdView/cmdView.js


const queryAPI = requirePlugin("hello-plugin")
const setAPI = requirePlugin("hello-plugin")

const {
	handleToast,
	closeToast
} = require('../../../utils/alertUtil')
const constant = require("../../../utils/constant")
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		canvasText: "接收到蓝牙设备信息:"
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {

	},
	/**
	 * 设备基本信息
	 */
	queryDeviceBasicInfo() {
		handleToast("查询设备的基本信息", "loading", 0)
		var that = this
		queryAPI.queryDeviceBasicInfo(function (state, response) {
			closeToast()
			that.setData({
				canvasText: that.data.canvasText + "\rbasicInfo: " + JSON.stringify(response)
			})
		})
	},
	queryDeviceMacAddress() {
		var that = this
		handleToast("查询设备的mac地址", "loading", 0)
		queryAPI.queryDeviceMacAddress(function (state, response) {
			closeToast()
			that.setData({
				canvasText: that.data.canvasText + "\rMAC:" + JSON.stringify(response)
			})
		})
	},
	/**
	 * 获取设备主题
	 */
	queryDeviceTheme() {
		var that = this
		handleToast("查询设备主题", "loading", 0)
		queryAPI.queryDeviceTheme(function (state, response) {
			closeToast()
			that.setData({
				canvasText: that.data.canvasText + "\rdeviceTheme: " + JSON.stringify(response)
			})
		})

	},
	/**
	 * 获取设备平台
	 */
	queryDeviceMcu() {
		var that = this
		handleToast("查询硬件设备平台", "loading", 0)
		queryAPI.queryDeviceMcu(function (state, response) {
			closeToast()
			that.setData({
				canvasText: that.data.canvasText + "\rdeviceMcu: " + JSON.stringify(response)
			})
		});
  },
  	/**
	 * 获取设备型号
	 */
	queryDevcieModel() {
		var that = this
		handleToast("查询设备型号信息", "loading", 0)
		queryAPI.queryDevcieModel(function (state, response) {
			closeToast()
			that.setData({
				canvasText: that.data.canvasText + "\r设备型号: " + JSON.stringify(response)
			})
		});
  },
  /**
   * 获取设备功能开关状态
   */
  queryDevcieModelStatus(){
		var that = this
		handleToast("获取设备功能开关状态", "loading", 0)
		queryAPI.queryDevcieModelStatus( (state, response) => {
			closeToast()
			that.setData({
				canvasText: that.data.canvasText + "\r设备功能开关状态: " + JSON.stringify(response)
			})
		});
  },
	/**
	 * 获取实时温度
	 */
	queryTemperature() {
		var that = this
		handleToast("查询设备当前温度", "loading", 0)
		queryAPI.queryTemperature(function (state, response) {
			closeToast()
			that.setData({
				canvasText: that.data.canvasText + "\rtemperature: " + JSON.stringify(response)
			})
		});
	},
	/**
	 * 获取设备闹钟信息
	 */
	queryAlarmClock() {
		var that = this
		handleToast("查询闹钟", "loading", 0)
		setAPI.queryDeviceAlarm(function (state, response) {
			closeToast()
			that.setData({
				canvasText: that.data.canvasText + "\n获取闹钟信息: " + JSON.stringify(response)
			})
		});
	},

	/**
	 * // mark: 添加闹钟
	 */
	addAlarmClock() {
		var that = this
		handleToast("添加闹钟", "loading", 0)
		let repeat = [
			constant.repeatMonday,
			constant.repeatTuesday,
			constant.repeatWednesday,
			constant.repeatThursday,
			constant.repeatFriday,
			constant.repeatSaturday,
			constant.repeatSunday,
			constant.repeatEnable,
		]
		setAPI.addDeviceAlarm(
			constant.alarmTypeWakeUp,
			7,
			30,
			repeat,
			0,
			function (state, response) {
				closeToast()
				that.setData({
					canvasText: that.data.canvasText + "\n增加闹钟: " + JSON.stringify(response)
				})
			}
		);

	},

	/**
	 * 修改闹钟
	 */
	modityAlarmClock() {
		var that = this
		handleToast("修改闹钟", "loading", 0)

		setAPI.modifyDeviceAlarm(7, 30, 12, 0, constant.alarmTypeMeeting, [
				constant.repeatMonday,
				constant.repeatTuesday,
				constant.repeatWednesday,
				constant.repeatThursday,
				constant.repeatFriday,
				constant.repeatEnable,
			],
			0,
			function (state, response) {
				closeToast()
				that.setData({
					canvasText: that.data.canvasText + "\n修改闹钟: " + JSON.stringify(response)
				})
			}
		);
	},

	/**
	 * 删除闹钟
	 */
	deleteAlarmClock() {

		var that = this
		handleToast("删除闹钟", "loading", 0)
		setAPI.deleteDeviceAlarm(12, 0, function (state, response) {
			closeToast()
			that.setData({
				canvasText: that.data.canvasText + "\n删除闹钟: " + JSON.stringify(response)
			})
		});
  },

/**
   * 获取计数值
   */
  queryCountValue(){
		var that = this
		handleToast("获取计数值", "loading", 0)
		queryAPI.queryCountValue((state, response) => {
			closeToast()
			that.setData({
				canvasText: that.data.canvasText + "\n获取计数值: " + JSON.stringify(response)
			})
		});
  },

  /**
   * 清除计数值
   */
  clearCountValue(){
		var that = this
		handleToast("清除计数值", "loading", 0)
		setAPI.clearCountValue((state, response) => {
      closeToast()
      if (state == constant.succeed){
			that.setData({
				canvasText: that.data.canvasText + "\n清除计数值: " + "succeed"
      })
    } else{
      that.setData({
				canvasText: that.data.canvasText + "\n清除计数值: " + "failed"
      })
    }
      
		});
  },

  
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})